package com.you.weixinpay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.you.weixinpay.domain.entity.TOrderInfo;
import com.you.weixinpay.domain.entity.TRefundInfo;
import com.you.weixinpay.mapper.TRefundInfoMapper;
import com.you.weixinpay.service.TOrderInfoService;
import com.you.weixinpay.service.TRefundInfoService;
import com.you.weixinpay.utils.OrderNoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yzm
 * @since 2025-06-15
 */
@Service
public class TRefundInfoServiceImpl extends ServiceImpl<TRefundInfoMapper, TRefundInfo> implements TRefundInfoService {

    @Autowired
    private TOrderInfoService orderInfoService;

    @Override
    public TRefundInfo createRefundByOrderNo(String orderNo, String reason) {
        //根据订单号获取订单信息
        TOrderInfo orderInfo = orderInfoService.getOne(new LambdaQueryWrapper<TOrderInfo>().eq(TOrderInfo::getOrderNo, orderNo));

        //根据订单号生成退款订单
        TRefundInfo refundInfo = new TRefundInfo();
        refundInfo.setOrderNo(orderNo);//订单编号
        refundInfo.setRefundNo(OrderNoUtils.getType(orderInfo.getPaymentType())+OrderNoUtils.getRefundNo());//退款单编号
        refundInfo.setTotalFee(orderInfo.getTotalFee());//原订单金额(分)
        refundInfo.setRefund(orderInfo.getTotalFee());//退款金额(分)
        refundInfo.setReason(reason);//退款原因

        //保存退款订单
        baseMapper.insert(refundInfo);

        return refundInfo;
    }
}
